package org.zhss.demo.redis.geo;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;

import java.util.List;

/** 查找附近的人案例 */
public class NearbyShopsDemo {

  private Jedis jedis = new Jedis("192.168.150.201");

  /**
   * 添加一个地理位置
   *
   * @param name
   * @param longitude
   * @param latitude
   */
  public void addLocation(String name, double longitude, double latitude) {
    jedis.geoadd("location_data", longitude, latitude, name);
  }

  /**
   * 查找附近5公里内的店铺
   *
   * @return
   */
  public List<GeoRadiusResponse> getNearByShops() {
    return jedis.georadiusByMember("location_data", "张三", 5, GeoUnit.KM);
  }

  public static void main(String[] args) {
    NearbyShopsDemo demo = new NearbyShopsDemo();
    List<GeoRadiusResponse> nearByShops = demo.getNearByShops();
    nearByShops.forEach(
        GeoRadiusResponse ->
            System.out.println("附近5公里的店铺为：" + GeoRadiusResponse.getMemberByString()));
  }
}
